package com.example.spring.jpa.specification;

import com.example.spring.jpa.ast.PriorityOperation;

import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
/**
 * @author: orange
 * @create: 2023-07-24 20:16
 * @description:
 */
public class FloatGenerate extends NumberGenerate {
    @Override
    public Number getNumber(String right) {
        return Float.valueOf(right);
    }

    @Override
    public Expression<Float> getExpression(GenerateApplicationContext context) {
        if (context.getPath() != null) {
            return context.getRoot().as(Float.class);
        }
        return context.getRoot().get(context.getLeft()).as(Float.class);
    }

    @Override
    public Predicate childGenerate(GenerateApplicationContext context) {
        if (PriorityOperation.GTE.getOperation().equals(context.getOperation())) {
            return context.getBuilder().greaterThanOrEqualTo(getExpression(context), Float.valueOf(context.getRight()));
        } else if (PriorityOperation.LTE.getOperation().equals(context.getOperation())) {
            return context.getBuilder().lessThanOrEqualTo(getExpression(context), Float.valueOf(context.getRight()));
        }
        throw new RuntimeException(String.format("Float类型不支持%s", context.getOperation()));
    }
}
